package net.fornwall.eclipsecoder.actions;

import net.fornwall.eclipsecoder.arena.EclipseCoderEntryPoint;
import net.fornwall.eclipsecoder.util.Utilities;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;

public class SubmitAction implements IWorkbenchWindowActionDelegate {

	static IAction theAction = new Action("Submit", PlatformUI.getWorkbench()
			.getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_UP)) {
		{
			setToolTipText("Submit the current problem");
			setEnabled(false);
		}

		@Override
		public void run() {
			EclipseCoderEntryPoint.getInstance().submit();
		}
	};

	public static IAction getAction() {
		return theAction;
	}

	/**
	 * Thread-safe modification of status.
	 */
	public static void setEnabled(final boolean enabled) {
		Utilities.runInDisplayThread(new Runnable() {
			public void run() {
				getAction().setEnabled(enabled);
			}
		});
	}

	/**
	 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
	 */
	public void dispose() {
		// do nothing
	}

	/**
	 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
	 */
	public void init(IWorkbenchWindow window) {
		// do nothing
	}

	/**
	 * Submit solution string.
	 * 
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	public void run(IAction action) {
		if (theAction.isEnabled()) {
			EclipseCoderEntryPoint.getInstance().submit();
		}
	}

	/**
	 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
	 *      org.eclipse.jface.viewers.ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		// do nothing
	}

}